/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.gt.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.constant.Constant;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.UserAgentUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.gt.entity.GtInfoDeliver;
import com.thinkgem.jeesite.modules.gt.entity.GtWorkReport;
import com.thinkgem.jeesite.modules.gt.service.GtAnalysisService;
import com.thinkgem.jeesite.modules.gt.service.GtWorkReportService;
import com.thinkgem.jeesite.modules.sys.entity.Area;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.AreaService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 工作月报信息Controller
 * @author zad
 * @version 2018-03-26
 */
@Controller
@RequestMapping(value = "${frontPath}/gt/gtAnalysis")
public class GtAnalysisController extends BaseController {

	@Autowired
	private GtWorkReportService gtWorkReportService;
	@Autowired
	private AreaService areaService;
	
	@Autowired
	private GtAnalysisService gtAnalysisService;
	
	@RequestMapping("chart")
	public String chart(String menuId,String category,Area area, HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("menuId", menuId);
		
		model.addAttribute("category", category);
		
		User user = UserUtils.getUser();
		model.addAttribute("user", user);
		String type = user.getArea().getType();
		
		// 查询地区组织机构
		Area param = new Area();
		Area p = new Area();
		p.setCode("sd");
		param.setParent(p);
		param.setType(Constant.Area.AREA_TYPE_REGION);
		List<Area> regions = this.areaService.findList(param);
		model.addAttribute("regions", regions);
		
		Map<String,String> mapRs = new HashMap<String, String>();
		
		
		Map<String,String> map = new HashMap<String, String>();
		// String code = user.getArea().getCode();
		if(null != area && StringUtils.isNotBlank(area.getCode()) && StringUtils.isNotBlank(area.getType()))
		{
			map.put("code", area.getCode());
			map.put("level", area.getType());
		}
		else
		{
			// 省或者地市管理员
			if(Constant.Area.AREA_TYPE_PROVINCE.equals(type) || Constant.Area.AREA_TYPE_REGION .equals(type))
			{
				map.put("code", "sd");
				map.put("level", Constant.Area.AREA_TYPE_PROVINCE);
				
				// 设置默认地市
				if(Constant.Area.AREA_TYPE_REGION .equals(type))
				{
					area = user.getArea();
					map.put("code", area.getCode());
					map.put("level", Constant.Area.AREA_TYPE_REGION);
				}
			}
			// 区县管理员时，默认为该区县管理员上级
			else
			{
				/*Area a = regions.get(0);
				map.put("code", a.getCode());
				map.put("level", a.getType());*/
				area = user.getArea().getParent();
				map.put("code", area.getCode());
				map.put("level", area.getType());
				
				
			}
		}
		model.addAttribute("area", area);
		
		// 经济林统计
		if("forest".equals(category))
		{
			// 各地市、区县扶贫林进度统计(面积、人数完成进度)
			List<Map<String,String>> forestRegionList = this.gtAnalysisService.forestRegionRate(map);
			// model.addAttribute("forestRegionList", forestRegionList);
			
			// 省管理员
			if(Constant.Area.AREA_TYPE_PROVINCE.equals(type))
			{
				model.addAttribute("forestRegionList", forestRegionList);
			}
			
			mapRs = this.gtAnalysisService.parseMapChart(forestRegionList, "name", "forestRate", "peopleRate","");
			mapRs.put("rate1Name", "面积");
			mapRs.put("rate2Name", "人数");
		}
		// 护林员岗位统计
		else if("post".equals(category))
		{
			List<Map<String,String>> postRegionList = this.gtAnalysisService.postRegionPeopleRate(map);
			
			// 省管理员
			if(Constant.Area.AREA_TYPE_PROVINCE.equals(type))
			{
				model.addAttribute("postRegionList", postRegionList);
			}
			
			mapRs = this.gtAnalysisService.parseMapChart(postRegionList, "name", "forestRate", "rate","");
			this.gtAnalysisService.replaceKey(mapRs, "rates", "peopleRates");
			mapRs.put("rate2Name", "人数");
		}
		// 林业政策统计
		/*else if("policy".equals(category))
		{
			
		}*/
		// 企业合作社统计
		else if("company".equals(category))
		{
			List<Map<String,String>> companyRegionRate = this.gtAnalysisService.companyRegionRate(map);
			
			// 省管理员
			if(Constant.Area.AREA_TYPE_PROVINCE.equals(type))
			{
			model.addAttribute("companyRegionRate", companyRegionRate);
			}
						
			mapRs = this.gtAnalysisService.parseMapChart(companyRegionRate, "name", "forestRate", "rate","");
			this.gtAnalysisService.replaceKey(mapRs, "rates", "peopleRates");
			mapRs.put("rate2Name", "企业（合作社）");
		}
		// 科技下乡统计
		else if("country".equals(category))
		{
			List<Map<String,String>> countryRegionRate = this.gtAnalysisService.countryRegionRate(map);
			
			// 省管理员
			if(Constant.Area.AREA_TYPE_PROVINCE.equals(type))
			{
			model.addAttribute("countryRegionRate", countryRegionRate);
			}
						
			mapRs = this.gtAnalysisService.parseMapChart(countryRegionRate, "name", "forestRate", "countryRate","countryCoverRate");
			this.gtAnalysisService.replaceKey(mapRs, "countryRates", "peopleRates");
			this.gtAnalysisService.replaceKey(mapRs, "countryCoverRates", "clearCountryRates");
			String level = map.get("level");
			// 省级时，下级为地区
			if(Constant.Area.AREA_TYPE_PROVINCE.equals(level))
			{
				mapRs.put("rate2Name", "地市4次");
				mapRs.put("rate3Name", "覆盖村");
			}
			if(Constant.Area.AREA_TYPE_REGION.equals(level))
			{
				mapRs.put("rate2Name", "覆盖村");
			}
		}
		// 技术培训统计
		/*else if("train".equals(category))
		{
			
		}*/
		// 技术明白人统计
		else if("tecClear".equals(category))
		{
			map.put("clearType", "4");
			map.put("clearTypeTown", "5");
			map.put("clearTypeCountry", "6");
			List<Map<String,String>> countryRegionRate = this.gtAnalysisService.regionCountyTownClearRate(map);
			
			// 省管理员
			if(Constant.Area.AREA_TYPE_PROVINCE.equals(type))
			{
			model.addAttribute("countryRegionRate", countryRegionRate);
			}
						
			// chart页面对应读取key值："forestRates", "peopleRates,clearCountryRates"
			mapRs = this.gtAnalysisService.parseMapChart(countryRegionRate, "name", "clearRate", "clearTownRate","clearCountryRate");
			
			this.gtAnalysisService.replaceKey(mapRs, "clearRates", "forestRates");
			this.gtAnalysisService.replaceKey(mapRs, "clearTownRates", "peopleRates");
			mapRs.put("rate1Name", "县专家");
			mapRs.put("rate2Name", "乡镇技术员");
			mapRs.put("rate3Name", "村明白人");
			
		}
		
		model.addAttribute("mapRs", mapRs);
		
		return "modules/gt/basic/chart";
	}

	
	// 本级任务完成
	@RequestMapping("gtOverAll")
	public String gtOverAll(String menuId,String category,Area area, HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("menuId", menuId);
		
		Map<String,String> map = new HashMap<String, String>();
		
		User user = UserUtils.getUser();
		String code = user.getArea().getCode();
		map.put("code", code);
		String type = user.getArea().getType();
		map.put("level", type);
		
		// 不再查询全省，根据用户的实际单位来查询
		// map.put("code", "sd");
		// map.put("level", "2");
		
		// add by zad 2018-04-08 经济扶贫林总体进度
		Map<String,String> forestTotalRate = this.gtAnalysisService.forestTotalRate(map);
		forestTotalRate.put("forestAreaName", "经济林扶贫（面积：亩）");
		forestTotalRate.put("forestPeopleName", "经济林扶贫（人数）");
		model.addAttribute("forestTotalRate", forestTotalRate);
		
		
		// add by zad 2018-04-08 护林员岗位总体扶贫进度
		Map<String,String> postTotalRate = this.gtAnalysisService.postTotalRate(map);
		postTotalRate.put("postName", "护林员岗位扶贫");
		model.addAttribute("postTotalRate", postTotalRate);
		
		// add by zad 2018-04-08 林业政策总体扶贫进度
		Map<String,String> policyTotal = this.gtAnalysisService.policyTotal(map);
		policyTotal.put("policyName", "林业政策扶贫");
		model.addAttribute("policyTotal", policyTotal);
		
		// add by zad 2018-04-08 龙头企业（合作社）总体扶贫进度
		Map<String,String> companyTotal = this.gtAnalysisService.companyTotal(map);
		companyTotal.put("companyName", "富民示范龙头企业（合作社）扶贫");
		model.addAttribute("companyTotal", companyTotal);
		
		// add by zad 2018-04-26 科技下乡地区总体扶贫进度，省2次，地市4次、区县常态化
		/*Map<String,String> regionCompanyTotal = this.gtAnalysisService.regionCountryTotal(map);
		regionCompanyTotal.put("regionCountryName", "科技下乡扶贫（地市统计）");
		model.addAttribute("regionCompanyTotal", regionCompanyTotal);*/
		
		// add by zad 2018-04-26 技术培训总体扶贫进度，每个地市要求4次科技下乡活动
		Map<String,String> trainTotal = this.gtAnalysisService.trainTotal(map);
		trainTotal.put("trainName", "技术培训");
		model.addAttribute("trainTotal", trainTotal);
		
		// add by zad 2018-04-26 技术明白人总体扶贫进度，每个地市要求4次科技下乡活动
		/*Map<String,String> clearTotal = this.gtAnalysisService.clearTotal(map);
		clearTotal.put("clearName", "技术明白人");
		model.addAttribute("clearTotal", clearTotal);*/
		
		map.put("clearType", "4");
		map.put("clearTypeTown", "5");
		map.put("clearTypeCountry", "6");
		Map<String,String> clearTotal = this.gtAnalysisService.clearRegionTotal(map);
		clearTotal.put("clearName", "县技术明白人");
		clearTotal.put("clearTownName", "乡镇技术员");
		clearTotal.put("clearCountryName", "村技术明白人");
		model.addAttribute("clearTotal", clearTotal);
		
		
		// add by zad 2018-04-26 当月工作月报进度
		Map<String,String> reportTotal = this.gtAnalysisService.reportTotal(map);
		reportTotal.put("reportName", "工作月报（当月）");
		model.addAttribute("reportTotal", reportTotal);
		
		// add by zad 2018-04-26 当月工作月报进度
		Map<String,String> deliverTotal = this.gtAnalysisService.deliverTotal(map);
		deliverTotal.put("deliverName", "信息宣传");
		model.addAttribute("deliverTotal", deliverTotal);
		
		// 科技下乡查询，与下级查询调用同一个sql
		// map.put("level", String.valueOf(Integer.parseInt(type) - 1));
		map.put("level", String.valueOf(type));
		map.put("queryType", "self");
		Map<String,String> regionCompanyTotal = this.gtAnalysisService.countryRegionRate(map).get(0);
		regionCompanyTotal.put("regionCountryName", "科技下乡");
		model.addAttribute("regionCompanyTotal", regionCompanyTotal);
		
		return "modules/gt/basic/gtOverAll";
	}
	
	// 本级任务完成
	@RequestMapping("subRate")
	public String subRate(String menuId,String category,Area area, HttpServletRequest request, HttpServletResponse response, Model model) {

		
		// User user = UserUtils.getUser();
		// model.addAttribute("user", user);
		model.addAttribute("menuId", menuId);
		
		Map<String,String> map = new HashMap<String, String>();
		// String code = user.getArea().getCode();
		// map.put("code", code);
		
		area = this.areaService.get(area.getCode());
		
		map.put("code", area.getCode());
		
		// String type = user.getArea().getType();
		map.put("level", area.getType());
		
		
		// add by zad 2018-04-08 经济扶贫林总体进度  面积和人数
		Map<String,String> forestTotalRate = this.gtAnalysisService.forestTotalRate(map);
		model.addAttribute("forestTotalRate", forestTotalRate);
		// add by zad 2018-04-08 经济扶贫林总体进度
		/*Map<String,String> forestPeopleRate = this.gtAnalysisService.forestPeopleRate();
		model.addAttribute("forestPeopleRate", forestPeopleRate);*/
		
		// add by zad 2018-04-08 护林员岗位总体扶贫进度
		Map<String,String> postTotalRate = this.gtAnalysisService.postTotalRate(map);
		model.addAttribute("postTotalRate", postTotalRate);
		
		// add by zad 2018-04-08 企社总体扶贫进度          2018-04-20 企社不再区分是企业还是合作社
		// map.put("type", Constant.Company.COMPANY_TYPE_COMPANY);
		Map<String,String> companyTotalRate = this.gtAnalysisService.companyTotalRate(map);
		model.addAttribute("companyTotalRate", companyTotalRate);
		
		
		
		map.put("clearType", "4");
		map.put("clearTypeTown", "5");
		map.put("clearTypeCountry", "6");
		List<Map<String,String>> fpRegionAllRateList = this.gtAnalysisService.fpRegionAllRate(map);
		model.addAttribute("fpRegionAllRateList", fpRegionAllRateList);
		model.addAttribute("area", area);
		
		return "modules/gt/basic/subRate";
	}

}